<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ipc-event</title>
    <style>
        button {
            margin: 5px;
        }
    </style>
    <script type="application/javascript">
        let idx = 0;
        let si = null;
        let si1 = null;
        let si2 = null;
        console.log('window.chrome.webview:', window.chrome.webview)

        function autoIpcEmit() {
            if (si != null) {
                clearInterval(si)
                clearInterval(si1)
                clearInterval(si2)
                si = null
                si1 = null
                si2 = null
                idx = 0
                msg("停止 auto ipc emit idx", idx)
                return
            }
            msg("启动 auto ipc emit")
            si = setInterval(testEmitName, 1)
            si1 = setInterval(testEmitName, 1)
            //si2 = setInterval(testEmitName, 1)
            if (si != null) {
                let time = document.getElementById("time").value - 0
                if (time > 0) {
                    setTimeout(function () {
                        clearInterval(si)
                        clearInterval(si1)
                        clearInterval(si2)
                        si = null
                        si1 = null
                        si2 = null
                    }, time)
                }
            }
        }

        function testEmitName() {
            idx++;
            let objectValueArray = [
                100, 200, "数组里的字符串", 66996.99,
            ]
            let objectValue = {
                "stringField": "string 字符串字符串字符串字符串字符串字符串字符串字符串字string 符串字符串字符串字符串字符串字符串字符串字符串字符串字符串字符string 串字符串字符串字符串字符串字符string 串字符串字符串字符串string 字符串字符串字符串字符串字符串字符串字符串字符串字符串字符串字符串",
                "intField": 1000,
                "arrayField": objectValueArray,
                "doubleField": 999991.102,
                "booleanField": true,
            }
            let arrayValue = [
                100, 200, "数组里的字符串", 66996.99, objectValue, true, false
            ]
            let args1 = "中文中文中文中文中文中文中文string 中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文string 中文中文中文中文中文中文中文中文中文中文中文中文中string 文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文"
            let args2 = "第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数第二个参数"
            let ok = ipc.emit("testEmitName", [args1, args2, idx, true, 1999.66 + idx, "字符串？", 29999.66 + idx, objectValue, arrayValue, 8888888888888 + idx, null, undefined],
                function (r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10) {
                    clearMsg()
                    msg("R0：", r0);
                    msg("R1：", r1);
                    msg("R2：", r2);
                    msg("R3：", r3);
                    msg("R4：", r4);
                    msg("R5：", r5);
                    msg("R6：", r6, JSON.stringify(r6));
                    msg("R7：", r7, JSON.stringify(r7));
                    msg("R8：", r8, JSON.stringify(r8));
                    msg("R9：", r9, JSON.stringify(r9));
                    msg("R10：", r10, JSON.stringify(r10));
                });
            msg('ipc.emit testEmitName ok?', ok);

            //let ok1 = ipc.emit([]);
            // msg('ipc.emit ok?', ok1);
        }

        function testInArgs() {
            clearMsg()
            let in1 = "字符串参数"
            let in2 = 888888
            let in3 = 9999999.0111
            let in4 = true
            let in5 = ["value1", "字符串2", "字符串3"]
            let in6 = [1111111.1111, 222222.222, "这个数组需要使用 []any 类型接收"]
            let in7 = {
                "key1": "value1value1value1value1",
                "key2": "value2value2value2v2",
                "key3": "value3value3value3"
            }
            let in8 = {
                "key1": "value1value1value1value1",
                "key2": "value2value2value2vaaaaaaalue2",
                "key3": "value3value3value3",
                "key4": 1231,
                "key5": 1231.33,
                "key6": true,
                "subObj": {
                    "StringField": "StringFieldStringFieldStringField",
                    "IntField": 3333333,
                    "BoolField": true,
                    "FloatField": 1006588.666
                }
            }
            let in9 = {
                "in9key": in8
            }
            ipc.emit("testInArgs", [in1, in2, in3, in4, in5, in6, in7, in8, in9]);
        }

        function testNotInArgs() {
            ipc.emit("testNotInArgs")
        }

        function testNotInArgsRev() {
            ipc.emit("testNotInArgs", ['parame1这个参数没有接收'])
        }

        function testNotInArgsCallback() {
            ipc.emit("testNotInArgs", function (p1) {
                clearMsg()
                msg("有回调函数， 无返回值.", p1);
            })
        }

        function testResultArgs() {
            clearMsg()
            ipc.emit("testResultArgs", [6666], function (r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10) {
                msg("R0：", r0);
                msg("R1：", r1);
                msg("R2：", r2);
                msg("R3：", r3);
                msg("R4：", r4);
                msg("R5：", r5);
                msg("R6：", r6, JSON.stringify(r6));
                msg("R7：", r7, JSON.stringify(r7));
                msg("R8：", r8, JSON.stringify(r8));
                msg("R9：", r9, JSON.stringify(r9));
                msg("R10：", r10, JSON.stringify(r10));
            })
        }

        ipc.on('onTestName1', function (r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10) {
            clearMsg()
            msg("onTestName1");
            msg("R0：", r0);
            msg("R1：", r1);
            msg("R2：", r2);
            msg("R3：", r3);
            msg("R4：", r4);
            msg("R5：", r5);
            msg("R6：", JSON.stringify(r6));
            msg("R7：", JSON.stringify(r7));
            msg("R8：", JSON.stringify(r8));
            msg("R9：", JSON.stringify(r9));
            msg("R10：", JSON.stringify(r10));
        });

        let onTestName2Idx = 0;
        ipc.on('onTestName2', function (r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10) {
            clearMsg()
            onTestName2Idx++;
            msg("onTestName2--------------------------------");
            msg("R0：", r0);
            msg("R1：", r1);
            msg("R2：", r2);
            msg("R3：", r3);
            msg("R4：", r4);
            msg("R5：", r5);
            msg("R6：", JSON.stringify(r6));
            msg("R7：", JSON.stringify(r7));
            msg("R8：", JSON.stringify(r8));
            msg("R9：", JSON.stringify(r9));
            msg("R10：", JSON.stringify(r10));
            return ["返回值" + onTestName2Idx, onTestName2Idx, 2222.22 + onTestName2Idx, onTestName2Idx % 2 === 0]
        });

        function testGoToJSEvent() {
            ipc.emit("testGoToJSEvent")
        }

        ipc.on('notInArgs', function (r1) {
            msg("notInArgs", r1);
            return 1
        });

        let tgesi = null
        let tgesi1 = null

        function autotestGoEmit() {
            if (tgesi != null) {
                clearInterval(tgesi)
                clearInterval(tgesi1)
                tgesi = null
                tgesi1 = null
                idx = 0;
                return
            }
            tgesi = setInterval(testGoEmit, 1)
            tgesi1 = setInterval(testGoEmit, 1)
            if (tgesi != null) {
                let time = document.getElementById("time").value - 0
                if (time > 0) {
                    setTimeout(function () {
                        clearInterval(tgesi)
                        clearInterval(tgesi1)
                        msg(idx)
                        tgesi = null
                        tgesi1 = null
                        idx = 0;
                    }, time)
                }
            }
        }

        function testGoEmit() {
            idx++
            ipc.emit("testGoEmit", [idx]);
        }

        let atia = null

        function autoVariableTestInArgs() {
            if (atia != null) {
                clearInterval(atia)
                atia = null
                idx = 0;
                return
            }
            atia = setInterval(variableTestInArgs, 1)
            if (atia != null) {
                let time = document.getElementById("time").value - 0
                if (time > 0) {
                    setTimeout(function () {
                        clearInterval(atia)
                        msg(idx)
                        atia = null
                        idx = 0;
                    }, time)
                }
            }
        }

        function variableTestInArgs() {
            idx++
            let vara = ipc.emit("testResultArgs", [idx]);
            clearMsg()
            msg("variableTestInArgs, 返回true", vara);
        }


        let tgesic = null

        function autotestGoEmitAndCallback() {
            if (tgesic != null) {
                clearInterval(tgesic)
                tgesic = null
                idx = 0;
                return
            }
            tgesic = setInterval(testGoEmitAndCallback, 1)
            if (tgesic != null) {
                let time = document.getElementById("time").value - 0
                if (time > 0) {
                    setTimeout(function () {
                        clearInterval(tgesic)
                        msg(idx)
                        tgesic = null
                        idx = 0;
                    }, time)
                }
            }
        }

        function testGoEmitAndCallback() {
            clearMsg()
            ipc.emit("testGoEmitAndCallback");
        }

        let tessi = null

        function autoTestEmitSync() {
            if (tessi != null) {
                clearInterval(tessi)
                tessi = null
                idx = 0;
                return
            }
            tessi = setInterval(testEmitSync, 1)
            if (tessi != null) {
                let time = document.getElementById("time").value - 0
                if (time > 0) {
                    setTimeout(function () {
                        clearInterval(tessi)
                        msg(idx)
                        tessi = null
                        idx = 0;
                    }, time)
                }
            }
        }

        function testEmitSync() {
            clearMsg()
            let result = ipc.emitSync("testEmitSync", ["同步参数", 1, 2, 3, ["aaaa", "bbb", 6666]])
            msg("testEmitSync", JSON.stringify(result));
            // console.log("result:",result)
            ipc.emitSync("testEmitSync", ["同步参数", 1, 2, 3, ["aaaa", "bbb", 6666]], function (result1, result2) {
                msg("testEmitSync-callback result1:", result1, "result2:", result2);
            })
        }

        function testSendTargetGo() {
            let channelId = document.getElementById("channelId").value;
            ipc.emit("testSendTargetGo", [123456, "参数1", channelId, "多于的"])
        }

        // emit 选项配置
        function testEmitRule() {
            ipc.emit({
                name: "testOnTargetGo",                 //事件名
                arguments: [666666, process.frameId],   //参数, 获取当前进程 frameId
                callback: function (r1) {               //回调函数
                    msg("callback testEmitRule r1:", r1)
                },
                mode: 0, // int 0:async or 1:sync, default 0:async  触发模式、异步和同步
                target: 1, // int 0:main 1:current 2:other  触发目标, 主进程、当前进程、其它进程(需要主进程)
            })
        }

        let checki = null

        function autoCheckParam() {
            if (checki != null) {
                clearInterval(checki)
                checki = null
                return
            }
            checki = setInterval(check, 1)
            if (checki != null) {
                let time = document.getElementById("time").value - 0
                if (time > 0) {
                    setTimeout(function () {
                        clearInterval(checki)
                        checki = null
                    }, time)
                }
            }
        }

        let chkCount = 0;
        let pstr1 = "参数一1验证字符串参数一1验证字符串参数一1验证字符串参数一1验证字符串"
        let pint2 = 1212222
        let pint3 = 44444444
        let pfloat4 = 8888888
        let pany5 = 666666
        let pstr6 = "参数六6验证字符串参数验证字符串参数验证字符串参数验证字符串参数验证字符串参数"
        let pmap7 = {"key1": "key1StringValue", "key2": "888888", "key3": "999999.88"}
        let pstrarr8 = ["下标0参数", "下标1参数", "下标2参数", "下标3参数", "下标4参数"]
        let pintarr9 = [111111, 222222, 33333]
        let pbool10 = true

        function check() {
            chkCount++;
            clearMsg()
            msg("check-param", chkCount)
            ipc.emit("check-param", [pstr1, pint2, pint3, pfloat4, pany5, pstr6, pmap7, pstrarr8, pintarr9, pbool10])
            ipc.emit("check-param", [pstr1, pint2, pint3, pfloat4, pany5, pstr6, pmap7, pstrarr8, pintarr9, pbool10])
        }

        function reload() {
            ipc.emit("reload")
        }

        function SendMouseWheelEvent() {
            ipc.emit('SendMouseWheelEvent');
        }

        window.addEventListener("mousedown", e => {
            var v = window.getComputedStyle(e.target).getPropertyValue('--webkit-app-region');
            console.log("mousedown v:", v, "e:", e)
        });
    </script>
</head>
<body style="margin: 0px;padding: 0px;">
<p>
<h3 style="-webkit-app-region: drag;">process-message</h3></p>
<h3 style="color:red; --webkit-app-region: drag;">-customer</h3></p>
<div style="--webkit-app-region: drag;width: 100px;height: 100px;background-color: #702929">
    <div style="--webkit-app-region: no-drag;width: 30px;height: 30px;background-color: aliceblue"></div>
</div>

<p>
    <a target="_blank" href="ipc-event.html">open</a>
    <a href="https://www.baidu.com">百度</a>
    <img src="icon.ico" width="50" height="50">
</p>
<p>
    自动执行时间/毫秒: <input id="time" value="0"><br>
    通道ID:<input id="channelId" value="">
    <br>
    <button onclick="reload()">reload</button>
    <button onclick="check()">checkParam</button>
    <button onclick="autoCheckParam()">autoCheckParam</button>
    <br>
    <button onclick="testEmitName()">testEmitName</button>
    <button onclick="autoIpcEmit()">自动-testEmitName</button>
    <br>
    <button onclick="testInArgs()">只入参testInArgs</button>
    <button onclick="testResultArgs()">出参testResultArgs</button>
    <button onclick="testNotInArgs()">无入参/无出参testNotInArgs</button>
    <button onclick="testNotInArgsRev()">未接收参数/testNotInArgsRev</button>
    <button onclick="testNotInArgsCallback()">有回调函数无返回值/testNotInArgsCallback</button>
    <br>
    <button onclick="testGoToJSEvent()">GO/testGoToJSEvent</button>
    <br>
    <button onclick="testGoEmit()">testGoEmit</button>
    <button onclick="autotestGoEmit()">自动-testGoEmit</button>
    <br>
    <button onclick="variableTestInArgs()">variableTestInArgs</button>
    <button onclick="autoVariableTestInArgs()">自动variableTestInArgs</button>
    <br>
    <button onclick="testGoEmitAndCallback()">testGoEmitAndCallback</button>
    <button onclick="autotestGoEmitAndCallback()">自动-testGoEmitAndCallback</button>
    <br>
    <button onclick="testEmitSync()">testEmitSync</button>
    <button onclick="autoTestEmitSync()">自动-testEmitSync</button>
    <br>
    <button onclick="testSendTargetGo()">testSendTargetGo</button>
    <br>
    <button onclick="testEmitRule()">testEmitRule</button>
    <button onclick="SendMouseWheelEvent()">SendMouseWheelEvent</button>
</p>
<p id="msgHtml"></p>
</body>
<script type="application/javascript">
    let msgHtml = document.getElementById("msgHtml");

    function msg(...vvv) {
        let str = msgHtml.innerHTML;
        for (let i = 0; i < vvv.length; i++) {
            str = str + "  " + vvv[i];
        }
        str += "<br>";
        msgHtml.innerHTML = str;
    }

    function clearMsg() {
        msgHtml.innerHTML = "";
    }

    msg("ipc-event: 页面加载完成...");

    msg("process browserId", process.browserId)
    msg("process frameId", process.frameId)
</script>
</html>